<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var week="";
var monthCalendarData = [];
var inf="";
var idWrite = "";
var time;
//$(document).ready(function(){
//	var key=$("#selectRefundReason").val();
    //根据值让option选中 
//    $("#person_name_list option[value='"+key+"']").attr("selected","selected"); 
//});





$(function(){
	   var curr_time = new Date();
	   var str = curr_time.getFullYear()+"-";
	   str += curr_time.getMonth()+1+"-";
	   str += curr_time.getDate()+" ";
	   str += "00:00:00";
	   time = str;
	   
	   hide_type_level();
	   
	   schedule_show();
	
	
	$("#start_time").datetimebox({
		onHidePanel : function() {
			onChangeStartTime();
	    }
	});

	$("#end_time").datetimebox({
		onHidePanel : function() {
			onChangeEndTime();
	    }
	});
	
	
	
  //  isieBrowser();
    //解决ie不支持placeholder属性
    ;(function($){
    $.fn.placeholder = function(options){
    var opts = $.extend({}, $.fn.placeholder.defaults, options);
    var isIE = document.all ? true : false;
    return this.each(function(){
    var _this = this,
    placeholderValue =_this.getAttribute("placeholder");
    if(isIE){
    _this.setAttribute("value",placeholderValue);
    _this.onfocus = function(){
    $.trim(_this.value) == placeholderValue ? _this.value = "" : '';
    };
    _this.onblur = function(){
    $.trim(_this.value) == "" ? _this.value = placeholderValue : '';
    };
    }
    });
    };
    })(jQuery);
    $("input").placeholder();

    initHome();
    
    
    $("#add_programme").dialog({
    	closed:true,
    	modal:true,//
        width:720,
        height:440
    });
  
    
    function schedule_show(){
    	var lingdao = "${lingDao}";
    	if(lingdao == "1"){
    		$("#hide_type_show").show();
    		$("#hide_level_show").show();
    	}else{
    		$("#hide_type_show").hide();
    		$("#hide_level_show").hide();
    	}
    	$("#schedule_show").dialog({//主页点击今日日程自动弹出日程详情
        	closed:"${click_home}"==1?false:true,
        	modal:true,
            width:720,
            height:440
        });
    }
    
    
    
    
    $("#writeSchedule").dialog({
		closed:true,
		modal:true,
	    width:720,
	    height:440
    });
    
    
    
    function close(){
    	$("#schedule_show").dialog('close');
    }
    
    
    //编辑取消按钮
    $("#close").click(function(){
    	$("#schedule_show").dialog('close');
    });
    
    function showSchedule(){
    	$.ajax({
    		type: "post",
            url: "/document/schedule/scheduleMaintain",
            data:{},
            dataType: "json",
            success: function(data){
            }
    	});
    }
    
    showSchedule();
    
    
    
    //控制领导日程添加按钮和领导]日程列表
    function lingDaoButton(){
    	var boolscheduleAdmin = "${boolScheduleAdmin}";//管理权限
    	var lingDao = "${lingDao}";//领导
    	if(boolscheduleAdmin == "0" ){
    		$("#addlingdaoButton").hide();
    		if(lingDao == "0"){
    			$("#lindaoButton").hide();
    		}
    	}
    }
    
    lingDaoButton();
    
    
    //个人点击按钮
    $("#add_programme_btn").bind('click',function(){
    	var userName = "${userName}";
    	var lingdao = "${lingDao}";
    	if(lingdao == 1){
    		$("#hide_type").show();
    		$("#hide_level").show();
    		$("#hide_type_edit").show();
    		$("#hide_level_edit").show();
    		$("#hide_type_show").show();
    		$("#hide_level_show").show();
    	}else{
    		$("#hide_type").hide();
    		$("#hide_level").hide();
    		$("#hide_type_edit").hide();
    		$("#hide_level_edit").hide();
    		$("#hide_type_show").hide();
    		$("#hide_level_show").hide();
    	}
    	$("#add_programme").dialog('open');
    	$("#info_type").hide();
    	$("#person_name").show();
    	inf = "1";
       $("#person_name").focus();
    });
    
    //领导日程按钮
    $("#add_programme_btn1").bind('click',function(){
    	var userName = "${userName}";
    	$("#hide_type").show();
		$("#hide_level").show();
		$("#hide_type_edit").show();
		$("#hide_level_edit").show();
		$("#hide_type_show").show();
		$("#hide_level_show").show();
    	$("#add_programme").dialog('open');
    	$("#info_type").show();
    	$("#person_name").hide();
    	 inf = "2";
       $("#person_name").focus();
    });

    $("#cancel_add_calendar").bind('click',function(){
        hide();
    });
    
    $("#cancel_add_calendar_write").bind('click',function(){
        hide1();
        
    });

    $("#save_add_calendar").bind('click',function(){
        addCalendar();
    });
    
    $("#save_add_calendar_write").click(function(){
        addCalendar_write();
    });
    
    $('.easyui-datetimebox').datetimebox({
        showSeconds:true
    });
    
    //领导日程维护
    
    
})

function initHome(){
    getMonthCalendarList();
    getCalendarList();
}

//搜索****
function searchCalendarList(){
    var controlName, emptyMSG="";
    var search_content = $("#search_content").val();
    //alert(search_content);
    controlName = "calendarList";
    emptyMSG="<div style='text-align:center'>没有搜索到相关日程</div>";
    $.ajax({
        type: "post",
        url: "/document/schedule/month",
        data:{theme:search_content},
        dataType: "json",
        success: function(data){
            if(data.status == 200 ){
                searchResutList(data,controlName,emptyMSG);
            }
        }
    });
}




//显示搜索后的日程
function searchResutList(data,controlName,emptyMSG){
	var lingdao = "${lingDao}";
    var search_resut = 0;
    var searchList = false;
    var search_content = $("#search_content").val();
    if(search_content == null || search_content == ""){
        $.messager.alert('提示','请输入搜索内容');
        return;
    }
    var toDoListUl = $("#"+controlName).empty();
    var programme_data = JSON.parse(data.data); //转换为json数组
    if(programme_data.length > 0){
        $.each(programme_data, function(i, todoIssue){
            var formId = todoIssue.id;              //form ID
            var issueName = todoIssue.theme;        //日志主题
            var calendarType = "";      //日程类型
            var content = todoIssue.content;        //日志内容
            var startDate = todoIssue.startime;     //开始时间
            var endDate = todoIssue.endtime;        //结束时间
            var dateString = "";
            var startTime = "";
            var endTime = "";
            if(lingdao == "0"){
            	calendarType = "";
        	}else{
        		calendarType = todoIssue.type;      //日程类型
        	}
            startTime=startDate.substring(11,16);
            endTime=endDate.substring(11,16);
            dateString = startDate.substring(0,10);
            var todoUrl = "";
            var doIssueText = "";
            getWeek(dateString);
            if(1==1){//search_content == issueName
                search_resut++;
                if(search_resut >3){
                    return;
                }
                var index = "";                        //序号
                if(search_resut <= 9){
                    index = '0'+search_resut;          //序号
                }
                if(controlName == 'calendarList'){
                    $("#calendar_type option").each(function () {
                        var scheduleType = $(this).val();
                        var scheduleText = $(this).text();
                        if(scheduleType == calendarType){
                        	if(lingdao == "0"){
                        		doIssueText = '<dl class="in_box_tow_list_dl">'+
                                '<dt>'+ index +'</dt>'+
                                '<dd>'+
                                    '<h2>'+ dateString +' '+week+' '+startTime+'-'+ endTime +'</h2>'+
                                    '<h3><p>'+ issueName +'</p></h3>'+
                                    '<p class="in_box_tow_list_dl_p">'+ content +'</p>'+
                                    '<a href="#" class="in_box_tow_list_dl_a" id='+formId+'>删除</a>'+
                                '</dd>'+
                            '</dl>';
                        	}else{
                        		doIssueText = '<dl class="in_box_tow_list_dl">'+
                                '<dt>'+ index +'</dt>'+
                                '<dd>'+
                                    '<h2>'+ dateString +' '+week+' '+startTime+'-'+ endTime +'</h2>'+
                                    '<h3><p>'+ issueName +'</p><span>'+ scheduleText +'</span></h3>'+
                                    '<p class="in_box_tow_list_dl_p">'+ content +'</p>'+
                                    '<a href="#" class="in_box_tow_list_dl_a" id='+formId+'>删除</a>'+
                                '</dd>'+
                            '</dl>';
                        	}                           
                        }
                    });
                }
                searchList =true;
                toDoListUl.append(doIssueText);
                $("#"+formId).on("click",function(){
                    delCalendar(formId);
                });
            }
        });
        if(searchList){
            $.messager.alert("提示","已搜索完成，请查看");
            return;
        }
        if(!searchList){
            $.messager.alert("提示","没有搜索到相关日程");
            return;
        }
    }else{
    	$.messager.alert("提示","没有搜索到相关日程");
        toDoListUl.append(emptyMSG);
    }
}



//编辑日程
function writeSchedule(id){
	//alert(id);
	var lingdao = "${lingDao}";
	if(lingdao == "0"){
		$("#hide_type_edit").hide();
		$("#hide_level_edit").hide();
	}else{
		$("#hide_type_edit").show();
		$("#hide_level_edit").show();
	}
	$("#writeSchedule").dialog('open');
	$('#writeSchedule').window('center');
	$.ajax({
		type:"post",
		url:"/document/schedule/write",
		data:{id:id},
		dataType: "json",
		success:function(data){
			if(data.status==200){
				writeScheduleData(data);
			}else{
				$.messager.alert("提示",data.message);
			}
		}
		
	});
}

function writeScheduleData(data){
	var writeData = data.data; //转换为json数组
	idWrite = writeData.id;
	$("#calendar_title_write").val(writeData.theme);
	$("#calendar_type_write").combobox('setValue',writeData.type);
	$("#calendar_priority_write").combobox('setValue',writeData.level);
	$("#calendar_priority_write").val(writeData.level);
	$("#start_time_write").datetimebox('setValue', formatterEDate(new Date(writeData.startime)));
    $("#end_time_write").datetimebox('setValue', formatterEDate(new Date(writeData.endtime)));
	$("#calendar_contents_write").val(writeData.content);
}

formatterSDate = function (date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"+ (date.getMonth() + 1);
    return date.getFullYear() + '-' + month + '-' + day+" "+00+":"+00+":"+00;
};

formatterEDate = function (date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"+ (date.getMonth() + 1);
    var hor = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
};




//获取每月日程列表
function getMonthCalendarList(){
    $.ajax({
        type: "post",
        url: "/document/schedule/month",
        dataType: "json",
        cache: false,
        success: function(data){
            if(data.status == 200 ){
                createMonthCalendarList(data);
            }
        }
    });
}

//获取每月日志列表
function createMonthCalendarList(data){
    var programme_data = JSON.parse(data.data); //转换为json数组
    if(programme_data.length > 0){
        $.each(programme_data, function(i, todoIssue){
            var datas = {};
            var theme = todoIssue.theme;        //日志主题
            datas.theme = theme;
            datas.id = todoIssue.id;
            var startDate = todoIssue.startime; //开始日期
                datas.startDate=startDate.substring(0,10);
                monthCalendarData.push(datas);
        });
    }
    $("#calendar_shedule").calendar_shedule({
        data: monthCalendarData
    })
}

//获取日程列表
function getCalendarList(){
    var controlName, emptyMSG="";
    //var name = "${uuid}";
    var search_content = $("#search_content").val();
    controlName = "calendarList";
    emptyMSG="<div style='text-align:center'>暂无日程</div>";
    $.ajax({
        type: "get",
        url: "/document/schedule/day?"+new Date().getTime(),
        cache: false,
        //data:{theme:search_content},
        dataType: "json",
        success: function(data){
            if(data.status == 200 ){
                createCalendarList(data,controlName,emptyMSG);
            }
        }
    });
}

//显示日程列表
function createCalendarList(data,controlName,emptyMSG){
	var lingdao = "${lingDao}";
    var toDoListUl = $("#"+controlName).empty();
    var programme_data = JSON.parse(data.data); //转换为json数组
    if(programme_data.length > 0){
        $.each(programme_data, function(i, todoIssue){
           // if(i>=3){return;}
            var index = i+1;                        //序号
            if(index <= 9){
                index = '0'+index;                        //序号
            }
            var calendarType = "";
            var formId = todoIssue.id;              //form ID
            var issueName = todoIssue.theme;        //日志主题
            var content = todoIssue.content;        //日志内容
            
            var startDate = todoIssue.startime;     //开始时间
            var endDate = todoIssue.endtime;        //结束时间
            var dateString = "";
            var startTime = "";
            var endTime = ""; 
        	calendarType = todoIssue.type;      //日程类型
            startTime=startDate.substring(11,16);
            endTime=endDate.substring(11,16);
            dateString = startDate.substring(0,10);
            var todoUrl = "";
            var doIssueText = "";
            getWeek(dateString);
        	var scheduleText = $("#calendar_type option[value='"+calendarType+"']").text();
        	if(scheduleText!=''){
        		scheduleText = "<span>"+scheduleText+"</span>";
        	}
        	doIssueText = '<dl class="in_box_tow_list_dl">'+
            '<dt>'+ index +'</dt>'+
            '<dd>'+
                '<h2>'+ dateString +' '+week+' '+startTime+'-'+ endTime +'</h2>'+
                '<h3><p>'+ issueName +'</p>'+ scheduleText +'</h3>'+
                '<p class="in_box_tow_list_dl_p">'+ content +'</p>'+
                '<a href="#" class="in_box_tow_list_dl_a" id='+formId+'>删除</a>'+
            '</dd>'+
            '</dl>';
            toDoListUl.append(doIssueText);
            $("#"+formId).on("click",function(){
                delCalendar(formId);
            });
        });
    }else{
        toDoListUl.append(emptyMSG);
    }
}


//删除日志
function delCalendar(formId){
	$.messager.confirm({
	 	title:'提示',
	 	msg:'确定要删除数据吗？',
	 	top:100,
	 	fn:function(r){
	 		if(r){
	 			$.ajax({
	 				type:"post",
	 				url: "/document/schedule/delete",
	                data:{id:formId},
	                dataType: 'json',
	                success:function(data){
	                	if(data.state=="200"){
	                		initHome();
	                		window.location='/document/schedule/scheduleMaintain';
	                	}else{
	                		$.messager.alert({
	                			title:'提示',
	                        	 msg:'删除失败！',
	                        	 icon: 'info',
	                        	 top:100
	                		});
	                	}
	                }
	 			});
	 		}
	 	}
	 });
	
	
	
	

    
}

//保存日志  保存的数据封装：以json字符串的形式返回页面填写的内容

function addCalendar(){
	//$("#info_list option:first").prop("selected", 'selected');//设置<select>默认为第一项  
	var uid = "${uuid}";
	var createPerson_uid = "${createPerson_uuid}";
	var updatePerson_uid = "${updatePerson_uuid}";
	var uid_list = document.getElementById("person_name_list").value;	
    var uuid="", name = "",person_name="",info_list="",calendartype = "",priority = "",start_times = "",end_times = "",title = "",contents = "";
    person_name = $("#person_name").val();
    person_name_list = $("#person_name_list option:selected").text();
    if(inf == "2"){
    	name = person_name_list;
    	uuid = uid_list;
    }if(inf == "1"){
    	name = person_name;
    	uuid = uid;
    }  
    calendartype = $("#calendar_type").val();
    priority =  $("#calendar_priority").val();
    start_times = $("#start_time").val();
    end_times = $("#end_time").val();
    title = $("#calendar_title").val();
    contents = $("#calendar_contents").val(); 
    
    var lingdao = "${lingDao}";
	if(lingdao == "0"){
		if((name !="") && (start_times != "") && (end_times != "") && (title != "") && (contents != "")){
	        hide();
	        $.ajax({
	            type: "POST",
	            url: "/document/schedule/save",
	            data:{s_createPerson_uuid:createPerson_uid, s_updatePerson_uuid:updatePerson_uid, uuid:uuid,person:name,type:calendartype,level:priority,content:contents,theme:title,startime:start_times,endtime:end_times},
	            dataType: 'json',
	            success: function(data)
	            {
	            	if(data.status == "200"){
	            		$.messager.alert("提示", "新增日程成功！", "info", function () {window.location='/document/schedule/scheduleMaintain';});
	            		
	            	}else{
	            		$.messager.confirm({
	            			title:'提示',
	            			msg:'新增日程失败！',
	            			top:100,
	            			fn:function(r){
	            				if(r){
	            					window.location.reload();
	            				}
	            			}
	            		});
	            	}
	            },
	            error: function()
	            {
	                $.messager.alert("提示","新增日程保存失败！");
	            }
	        });
	    }else{
	        $.messager.alert("提示","请填写日程信息！");
	    }
	}else{
		if((name !="") && (start_times != "") && (end_times != "") && (title != "") && (contents != "")){
	        hide();
	        $.ajax({
	            type: "POST",
	            url: "/document/schedule/save",
	            data:{s_createPerson_uuid:createPerson_uid, s_updatePerson_uuid:updatePerson_uid, uuid:uuid,person:name,type:calendartype,level:priority,content:contents,theme:title,startime:start_times,endtime:end_times},
	            dataType: 'json',
	            success: function(data)
	            {
	            	if(data.status == "200"){
	            		$.messager.alert("提示", "新增日程成功！", "info", function () {window.location='/document/schedule/scheduleMaintain';});
	            		
	            	}else{
	            		$.messager.confirm({
	            			title:'提示',
	            			msg:'新增日程失败！',
	            			top:100,
	            			fn:function(r){
	            				if(r){
	            					window.location.reload();
	            				}
	            			}
	            		});
	            	}
	            },
	            error: function()
	            {
	                $.messager.alert("提示","新增日程保存失败！");
	            }
	        });
	    }else{
	        $.messager.alert("提示","请填写日程信息！");
	    }
	}
    
}



//去除隐藏层
function hide()
{
   //$("#hidebg").css('display','none');
   $('#add_programme').dialog('close');
}

function hide1(){
	$('#writeSchedule').dialog('close');
}

//获取星期

function getWeek(dateString){
    var date;
        var dateArray = dateString.split("-");
        date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
        week =  "星期" + "日一二三四五六".charAt(date.getDay());
    return week;
}


//编辑保存按钮
function addCalendar_write(){
	var person = $("#person_name_write").val();
	var type = $("#calendar_type_write").val();
	var level = $("#calendar_priority_write").val();
	var startime = $("#start_time_write").val();
	var endtime = $("#end_time_write").val();
	var theme = $("#calendar_title_write").val();
	var content = $("#calendar_contents_write").val();
	var uid = "${uuid}";
	var createPerson_uid = "${createPerson_uuid}";
	var updatePerson_uid = "${updatePerson_uuid}";
	if((uid!="") && (createPerson_uid!="") && (updatePerson_uid!="") && (idWrite!="") && (person!="") && (type!="") && (level!="") && (startime!="") && (endtime!="") && (theme!="") && (content!="")){
		$.ajax({
			type:"POST",
			url:"/document/schedule/write1",
			data:{uuid:uid, s_createPerson_uuid:createPerson_uid, s_updatePerson_uuid:updatePerson_uid, id:idWrite, person:person,type:type,level:level,startime:startime,endtime:endtime,theme:theme,content:content},
			dataType:'json',
			success:function(data){
				if(data.status == "200"){
					$.messager.alert("提示", "编辑成功！", "info", function () {window.location='/document/schedule/scheduleMaintain'; });
				}
			},
			error:function(){
				alert("编辑失败！");
				window.location.reload();
				
			}
		});
	}else{
		//window.location.reload();
		 //$.messager.alert("提示","编辑失败");
		alert("编辑失败！");
		window.location.reload();
	}
	 hide1();
	 
}

function onChangeStartTime(){
	var startTime = $("#start_time").val();//开始时间
	var endTime = $("#end_time").val();//结束时间
	if(startTime!=null&&startTime!=""&&endTime!=null&&endTime!=""){
//		if(startTime>time){
//			alert("q111");
//			$.messager.alert('提示','开始时间不能为已过时间!');
//			 $('#start_time').datetimebox('setValue', ''); 
//			 return;
//		}
		if(startTime>endTime){
			 $.messager.alert('提示','开始时间不能大于结束时间!');
			 $('#start_time').datetimebox('setValue', ''); 
			 return;
		}
	}	
}

function onChangeEndTime(){
	var startTime = $("#start_time").val();//开始时间
	var endTime = $("#end_time").val();//结束时间
	if(startTime!=null&&startTime!=""&&endTime!=null&&endTime!=""){
		if(startTime>endTime){
			 $.messager.alert('提示','开始时间不能大于结束时间!');
			 $('#end_time').datetimebox('setValue', ''); 
			 return;
		}
	}	
}

function hide_type_level(){
	var lingdao = "${lingDao}";
	if(lingdao == "0"){
		//$("#hide_type").hide();
		//$("#hide_level").hide();
		//$("#hide_type_edit").hide();
		//$("#hide_level_edit").hide();
		//$("#hide_type_show").hide();
		//$("#hide_level_show").hide();
	}
}


function isieBrowser(){
    if (window.ActiveXObject || "ActiveXObject" in window){
        $("#schedule_type").addClass("schedule_select");
        $("#schedule_priority").addClass("priority_select");
    }else{
        $("#schedule_type select").addClass("select_option_type");
        $("#schedule_priority select").addClass("select_option_priority");
    }
}